<!--
文件名：StudentList.vue
作者：教务系统开发团队
创建日期：2025-04-27
描述：学生列表组件，用于展示和管理学生信息
-->
<template>
  <div class="student-list-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>学生信息管理</span>
        <el-button 
          type="primary" 
          icon="el-icon-plus" 
          @click="handleCreate"
          style="float: right; padding: 3px 0"
        >
          新增学生
        </el-button>
      </div>
      
      <!-- 搜索表单 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="学号">
          <el-input v-model="searchForm.studentId" placeholder="请输入学号" clearable></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="searchForm.name" placeholder="请输入姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="searchForm.className" placeholder="请输入班级" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      
      <!-- 学生列表表格 -->
      <el-table 
        :data="studentList" 
        border 
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column prop="studentId" label="学号" width="120"></el-table-column>
        <el-table-column prop="name" label="姓名" width="100"></el-table-column>
        <el-table-column prop="gender" label="性别" width="80">
          <template slot-scope="scope">
            {{ scope.row.gender === 'M' ? '男' : '女' }}
          </template>
        </el-table-column>
        <el-table-column prop="className" label="班级" width="120"></el-table-column>
        <el-table-column prop="campus" label="校区" width="120"></el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag 
              :type="scope.row.status === 'active' ? 'success' : 'info'"
            >
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button 
              size="mini" 
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button 
              size="mini" 
              type="danger" 
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        style="margin-top: 20px; text-align: right"
      ></el-pagination>
    </el-card>
    
    <!-- 学生信息对话框 -->
    <student-form-dialog 
      :visible.sync="dialogVisible"
      :form-data="currentStudent"
      :dialog-type="dialogType"
      @refresh="fetchStudentList"
    />
  </div>
</template>

<script>
import { getStudents, deleteStudent } from '@/api/student'
import StudentFormDialog from './StudentFormDialog.vue'

export default {
  name: 'StudentList',
  components: {
    StudentFormDialog
  },
  data() {
    return {
      // 搜索表单
      searchForm: {
        studentId: '',
        name: '',
        className: ''
      },
      // 学生列表数据
      studentList: [],
      // 加载状态
      loading: false,
      // 分页信息
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      // 当前操作的学生
      currentStudent: {},
      // 对话框显示状态
      dialogVisible: false,
      // 对话框类型：create/edit
      dialogType: 'create'
    }
  },
  created() {
    this.fetchStudentList()
  },
  methods: {
    /**
     * 获取学生列表
     */
    async fetchStudentList() {
      this.loading = true
      try {
        const params = {
          ...this.searchForm,
          page: this.pagination.currentPage,
          page_size: this.pagination.pageSize
        }
        const res = await getStudents(params)
        this.studentList = res.data.results
        this.pagination.total = res.data.count
      } catch (error) {
        console.error('获取学生列表失败:', error)
        this.$message.error('获取学生列表失败')
      } finally {
        this.loading = false
      }
    },
    
    /**
     * 处理搜索
     */
    handleSearch() {
      this.pagination.currentPage = 1
      this.fetchStudentList()
    },
    
    /**
     * 重置搜索
     */
    resetSearch() {
      this.searchForm = {
        studentId: '',
        name: '',
        className: ''
      }
      this.handleSearch()
    },
    
    /**
     * 处理新增学生
     */
    handleCreate() {
      this.currentStudent = {}
      this.dialogType = 'create'
      this.dialogVisible = true
    },
    
    /**
     * 处理编辑学生
     * @param {Object} row - 学生信息
     */
    handleEdit(row) {
      this.currentStudent = { ...row }
      this.dialogType = 'edit'
      this.dialogVisible = true
    },
    
    /**
     * 处理删除学生
     * @param {Object} row - 学生信息
     */
    handleDelete(row) {
      this.$confirm(`确定要删除学生 ${row.name} 吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await deleteStudent(row.id)
          this.$message.success('删除成功')
          this.fetchStudentList()
        } catch (error) {
          console.error('删除学生失败:', error)
          this.$message.error('删除学生失败')
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    
    /**
     * 获取状态文本
     * @param {String} status - 状态值
     * @returns {String} 状态文本
     */
    getStatusText(status) {
      const statusMap = {
        active: '在读',
        graduated: '已毕业',
        transferred: '已转学',
        suspended: '休学'
      }
      return statusMap[status] || status
    },
    
    /**
     * 处理每页条数变化
     * @param {Number} val - 每页条数
     */
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.fetchStudentList()
    },
    
    /**
     * 处理当前页变化
     * @param {Number} val - 当前页码
     */
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.fetchStudentList()
    }
  }
}
</script>

<style scoped>
.student-list-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}
</style>